package com.glsc.ngateway.common.api.common.utils.encrypt;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AESEncrypt {

    private static byte[] keyBytes;

    @Value("${encrypt.aes.key}")
    public void SetPublicKey(String publicKey){
        AESEncrypt.keyBytes = Base64.decodeBase64(publicKey);
    }
    /**
     * 加密
     */
    public static String encrypt(String content) {
//        byte[] keyBytes = Base64.decodeBase64(publicKey);
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, keyBytes);
        //加密为16进制表示
        return aes.encryptHex(content);
    }

    /**
     * 解密
     */
    public static String decrypt(String content){
//        byte[] keyBytes = Base64.decodeBase64(publicKey);
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, keyBytes);
        //加密为16进制表示
        return aes.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
    }
}
